﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using tinyweb.framework;
using System.IO;
using Bepo.Ext;

namespace Bepo.API
{
    public class JsHandler
    {
        public static string combined = null;
        public IResult Get()
        {
            if (combined == null)
            {
                // Combine js files
                var folder = HttpContext.Current.Server.MapPath("~/Content/js");
                var fi = new DirectoryInfo(folder);
                combined = fi.GetFiles("*.js", SearchOption.AllDirectories)
                    .OrderBy(x => x.FullName)
                    .Aggregate(string.Empty, (x, y) => x + File.ReadAllText(y.FullName) + Environment.NewLine + ";" + Environment.NewLine);

            }

            HttpContext.Current.WriteCompressed(combined, "text/javascript");
            return Result.None();
        }
    }
}